//
//  JXWelcomeController.swift
//  01-weiboSwift
//
//  Created by TwistFate on 16/2/29.
//  Copyright © 2016年 TwistFate. All rights reserved.
//

import UIKit
import SDWebImage
import SVProgressHUD
import SnapKit

class JXWelcomeController: UIViewController {

    var iconImageViewBottomCons: NSLayoutConstraint?
    
    override func viewDidLoad() {
        super.viewDidLoad()

        setupIcon()
        
        JXUserAcountViewModel.sharedInstance.loadUserInfo { (error) -> () in
            if error != nil {
                if error?.code == -1 {
                    
                    SVProgressHUD.showErrorWithStatus("accessToken没有值")
                }else if error?.code == -2 {
                    SVProgressHUD.showErrorWithStatus("uid没有值")
                }
                return
            }
            
            self.setupIcon()
        }
        
        prepareUI()
    }
    
    private func setupIcon() {
        if let string = JXUserAcountViewModel.sharedInstance.userAcount?.avatar_large {
            let url = NSURL(string: string)
            self.iconImageView.sd_setImageWithURL(url, placeholderImage: UIImage(named:"avatar_default_big"))
        }

    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        moveAnimation()
    }
    
    func prepareUI () {
        view.addSubview(bkgImageView)
        view.addSubview(iconImageView)
        view.addSubview(welcomeLabel)
        
        
        // 2.添加约束
        bkgImageView.translatesAutoresizingMaskIntoConstraints = false
        iconImageView.translatesAutoresizingMaskIntoConstraints = false
        welcomeLabel.translatesAutoresizingMaskIntoConstraints = false
        
        // 背景
        // VFL: 可视化
        /*
        format:
        H: 水平方向约束 | 父控件的左边  | 父控件的右边
        V: 垂直方向约束 | 父控件的顶部  | 父控件的底部
        */
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[bkgView]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["bkgView" : bkgImageView]))
        
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[bkgView]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["bkgView" : bkgImageView]))
        
        //  头像
        // CenterX和父控件CenterX重合
        view.addConstraint(NSLayoutConstraint(item: iconImageView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        // Bottom和父控件的底部距离160
        iconImageViewBottomCons = NSLayoutConstraint(item: iconImageView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -160)
        view.addConstraint(iconImageViewBottomCons!)
        
        view.addConstraint(NSLayoutConstraint(item: iconImageView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant:85))
        
        view.addConstraint(NSLayoutConstraint(item: iconImageView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant:85))
        
        // messageLabel
        // CenterX和头像的CenterX重合
        view.addConstraint(NSLayoutConstraint(item: welcomeLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconImageView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        // Top和头像的Bottom间距16
        view.addConstraint(NSLayoutConstraint(item: welcomeLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconImageView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))

    }
    
    
    private func moveAnimation () {
        iconImageViewBottomCons?.constant = -(UIScreen.mainScreen().bounds.height - 160)
        
        // 动画
        UIView.animateWithDuration(2.5, delay: 0.2, usingSpringWithDamping: 0.6, initialSpringVelocity: 3, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                
                // label渐变动画
                UIView.animateWithDuration(0.5, animations: { () -> Void in
                    self.welcomeLabel.alpha = 1
                    }, completion: { (_) -> Void in
                        let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate
                        appDelegate?.switchRootController(true)
                })//
                
        }

    }

    lazy var bkgImageView : UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    
    lazy var iconImageView : UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "avatar_default_big"))
        imageView.layer.cornerRadius = 42.5
        imageView.layer.masksToBounds = true
        return imageView

        
    }()//UIImageView(image: UIImage(named: "avatar_default_big"))
    
    lazy var welcomeLabel : UILabel = {
        let label = UILabel()
        label.text = "欢迎回来"
        label.font = UIFont.systemFontOfSize(18)
//        label.sizeToFit()
        label.alpha = 0
        return label
    }()
}
